<template>
  <div>
    <vxe-toolbar
      id="toolbarId_bi4"
      :custom="{ storage: true }"
      export
      :refresh="{ query: findList }"
    >
      <template v-slot:buttons>
        <el-button @click="funcFilter">查询</el-button>
        <el-button v-if="!parentData.isDetail" @click="funcAdd">新增</el-button>
      </template>
    </vxe-toolbar>
    <vxe-table
      ref="xTable"
      border
      resizable
      stripe
      highlight-current-row
      highlight-hover-row
      row-id="id"
      height="700"
      :data="tableData"
      :loading="loading"
      :sort-config="{ remote: true }"
      export-config
      show-overflow
      align="center"
      @sort-change="sortList"  
    >
      <vxe-table-column type="checkbox" width="60"  fixed="left" />
      <vxe-table-column>
        <vxe-table-column field="consignorAddress" min-width="150" title="装货地址" sortable />
        <template v-slot:header>
          <el-cascader
            v-model="consignorAreaList"
            :options="parentData.areaOptions"
            :props="{ value: 'countyCode', label: 'countyName' }"
            clearable
            @change="changeConsignorAreaList"
          />
        </template>
      </vxe-table-column>
      <vxe-table-column>
        <vxe-table-column field="consignorDetailAddress" min-width="150" title="详细装货地址" sortable />
        <template v-slot:header>
          <el-input v-model="listQuery.consignorDetailAddress" placeholder="详细装货地址" clearable />
        </template>
      </vxe-table-column>
      <vxe-table-column>
        <vxe-table-column field="consignorName" min-width="150" title="装货人姓名" sortable />
        <template v-slot:header>
          <el-input v-model="listQuery.consignorName" placeholder="装货人姓名" clearable />
        </template>
      </vxe-table-column>
      <vxe-table-column>
        <vxe-table-column field="consignorPhone" min-width="150" title="装货人电话" sortable />
        <template v-slot:header>
          <el-input v-model="listQuery.consignorPhone" placeholder="装货人电话" clearable />
        </template>
      </vxe-table-column>
      <vxe-table-column>
        <vxe-table-column field="consigneeAddress" min-width="150" title="收货地址" sortable />
        <template v-slot:header>
          <el-cascader
            v-model="consigneeAreaList"
            :options="parentData.areaOptions"
            :props="{ value: 'countyCode', label: 'countyName' }"
            clearable
            @change="changeConsigneeAreaList"
          />
        </template>
      </vxe-table-column>
      <vxe-table-column>
        <vxe-table-column field="consigneeDetailAddress" min-width="150" title="详细收货地址" sortable />
        <template v-slot:header>
          <el-input v-model="listQuery.consigneeDetailAddress" placeholder="详细收货地址" clearable />
        </template>
      </vxe-table-column>
      <vxe-table-column>
        <vxe-table-column field="consigneeName" min-width="150" title="收货人姓名" sortable />
        <template v-slot:header>
          <el-input v-model="listQuery.consigneeName" placeholder="收货人姓名" clearable />
        </template>
      </vxe-table-column>
      <vxe-table-column>
        <vxe-table-column field="consigneePhone" min-width="150" title="收货人电话" sortable />
        <template v-slot:header>
          <el-input v-model="listQuery.consigneePhone" placeholder="收货人电话" clearable />
        </template>
      </vxe-table-column>
      <vxe-table-column>
        <vxe-table-column
          field="gmtCreated"
          title="创建时间"
          min-width="420"
          sortable
        />
        <template v-slot:header>
          <el-date-picker
            v-model="gmtCreatedRange"
            type="daterange"
            value-format="yyyy-MM-dd"
            :picker-options="pickerOptions"
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
            @change="changeDate"
          />
        </template>
      </vxe-table-column>
      <vxe-table-column v-if="!parentData.isDetail" title="操作" width="130"  field="action" fixed="right">
        <template slot-scope="scope">
          <el-tooltip class="item" effect="dark" content="编辑" placement="top">
            <vxe-button type="text" @click="funcEdit(scope.row.code)">
              <i class="el-icon-edit" />
            </vxe-button>
          </el-tooltip>
          <el-tooltip class="item" effect="dark" content="删除" placement="top">
            <vxe-button type="text" @click="funcDelete(scope.row.code)">
              <i class="el-icon-delete" />
            </vxe-button>
          </el-tooltip>
        </template>
      </vxe-table-column>
    </vxe-table>
    <vxe-pager
      :loading="loading"
      :current-page.sync="listQuery.pageNum"
      :page-size.sync="listQuery.pageSize"
      :total="listQuery.total"
      :page-sizes="Pagingconf"
      :layouts="['PrevJump', 'PrevPage', 'Number', 'NextPage', 'NextJump', 'Sizes', 'FullJump', 'Total']"
      @page-change="handlePageChange"
    />
    <vxe-modal v-model="modelAddFlag" title="新增" width="800" height="740" resize>
      <compAdd
        :area-options="parentData.areaOptions"
        :model-flag="modelAddFlag"
        @fatherAddMethod="fatherAdd"
        @fatherCancelMethod="fatherCancel"
      />
    </vxe-modal>
    <vxe-modal v-model="modelEditFlag" title="编辑" width="800" height="740" resize>
      <compEdit
        ref="editRef"
        :model-flag="modelEditFlag"
        :area-options="parentData.areaOptions"
        @fatherEditMethod="fatherEdit"
        @fatherCancelMethod="fatherCancel"
      />
    </vxe-modal>
  </div>
</template>
<script>
import compAdd from "./components/commonRouteInfoAdd.vue";
import compEdit from "./components/commonRouteInfoEdit.vue";
import { getCommonRouteInfoList, delCommonRouteInfo } from "@/api/shipment";
export default {
  name: "CommonRouteInfoList",
  components: {
    compAdd,
    compEdit
  },
  props: {
    parentData: {
      type: Object,
      default() {
        return {
          isDetail: false,
          areaOptions: [],
          goodsTypeList: [],
          typeOptions: [],
          vehicleDictList: [],
          bankOptions: []
        };
      }
    }
  },
  data() {
    return {
      pickerOptions: {
        shortcuts: [
          {
            text: "最近一周",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 7);
              picker.$emit("pick", [start, end]);
            }
          },
          {
            text: "最近一个月",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 30);
              picker.$emit("pick", [start, end]);
            }
          },
          {
            text: "最近三个月",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 90);
              picker.$emit("pick", [start, end]);
            }
          }
        ]
      },
      loading: false,
      consignorAreaList: [],
      consigneeAreaList: [],
      tableData: [],
      gmtCreatedRange: "",
      modelAddFlag: false,
      modelEditFlag: false,
      listQuery: {
        userCode: "",
        consignorProvinceCode: "",
        consignorCityCode: "",
        consignorCountyCode: "",
        consignorDetailAddress: "",
        consignorName: "",
        consignorPhone: "",
        consigneeProvinceCode: "",
        consigneeCityCode: "",
        consigneeCountyCode: "",
        consigneeDetailAddress: "",
        consigneeName: "",
        consigneePhone: "",
        gmtCreatedStart: "",
        gmtCreatedEnd: "",
        pageNum: 1,
        pageSize: 20,
        sortField: "",
        sortType: "",
        total: 0
      },
      Pagingconf: []
    };
  },
  created() {
    this.listQuery.userCode = this.$route.query.partyCode
      ? this.$route.query.partyCode
      : "";
    this.findList();
  },
  methods: {
    findList() {
      this.loading = true;
      return new Promise(resolve => {
        getCommonRouteInfoList(this.listQuery).then(response => {
          this.tableData = response.data.records;
          this.listQuery.total = response.data.total;
          this.Pagingconf = [20, 30, 50, 100, response.data.total||1];
          this.loading = false;
          resolve();
        });
      });
    },
    changeDate(value) {
      if (value) {
        this.listQuery.gmtCreatedStart = value[0] + " 00:00:00";
        this.listQuery.gmtCreatedEnd = value[1] + " 23:59:59";
      } else {
        this.listQuery.gmtCreatedStart = "";
        this.listQuery.gmtCreatedEnd = "";
      }
    },
    changeConsigneeAreaList(value) {
      this.listQuery.consigneeProvinceCode = value[0];
      this.listQuery.consigneeCityCode = value[1];
      this.listQuery.consigneeCountyCode = value[2];
    },
    changeConsignorAreaList(value) {
      this.listQuery.consignorProvinceCode = value[0];
      this.listQuery.consignorCityCode = value[1];
      this.listQuery.consignorCountyCode = value[2];
    },
    // 查询
    funcFilter() {
      this.listQuery.pageNum = 1;
      this.findList();
    },
    // 排序
    sortList({ column, property, order }) {
      var str;
      switch (property) {
        case "consignorAddress":
          str = "consignorCountyCode";
          break;
        case "consigneeAddress":
          str = "consigneeCountyCode";
          break;
        default:
          str = property;
          break;
      }
      if (order) {
        this.listQuery.sortField = str;
        this.listQuery.sortType = order;
      } else {
        this.listQuery.sortField = "";
      }
      this.findList();
    },
    // 添加方法
    funcAdd() {
      this.modelAddFlag = true;
    },
    // 修改方法
    funcEdit(code) {
      this.$refs.editRef.getInfo(code);
      this.modelEditFlag = true;
    },
    // 删除方法
    funcDelete(codes) {
      this.$confirm("确认删除吗？")
        .then(_ => {
          delCommonRouteInfo({ codes }).then(response => {
            this.$message({ message: response.msg, type: "success" });
            this.findList();
          });
        })
        .catch(_ => {});
    },
    // 增加组件的父方法
    fatherAdd() {
      this.modelAddFlag = false;
      this.findList();
    },
    // 编辑组件的父方法
    fatherEdit() {
      this.modelEditFlag = false;
      this.findList();
    },
    // 公共取消
    fatherCancel(modelFlag) {
      switch (modelFlag) {
        case "add":
          this.modelAddFlag = false;
          break;
        case "edit":
          this.modelEditFlag = false;
          break;
      }
    },
    // 分页handle
    handlePageChange({ currentPage, pageSize }) {
      this.listQuery.pageNum = currentPage;
      this.listQuery.pageSize = pageSize;
      this.findList();
    }
  }
};
</script>
